package com.geek.factory.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.entity.Device;
import com.geek.factory.result.Result;
import com.geek.factory.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/device")
@Api(tags = "设备管理")
@CrossOrigin
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @ApiOperation("查询所有设备（下拉等）")
    @GetMapping("/list")
    public Result getAll() {
        List<Device> list = deviceService.list();
        return new Result(SuccessConstant.SUCCESS, "查询成功", list);
    }

    @ApiOperation("分页查询设备")
    @PostMapping("/page")
    public Result getPage(
            @ApiParam(value = "当前页", required = true) @RequestParam(defaultValue = "1") Long now,
            @ApiParam(value = "每页大小", required = true) @RequestParam(defaultValue = "10") Long size,
            @ApiParam(value = "查询条件") @RequestBody(required = false) Device query
    ) {
        log.info("分页查询设备：页码={}, 大小={}, 条件={}", now, size, query);
        Page<Device> page = new Page<>(now, size);
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();

        if (query != null) {
            if (query.getName() != null) {
                wrapper.like(Device::getName, query.getName());
            }
            if (query.getCode() != null) {
                wrapper.like(Device::getCode, query.getCode());
            }
            if (query.getType() != null) {
                wrapper.eq(Device::getType, query.getType());
            }
            if (query.getProductiveId() != null) {
                wrapper.eq(Device::getProductiveId, query.getProductiveId());
            }
            if (query.getStatus() != null) {
                wrapper.eq(Device::getStatus, query.getStatus());
            }
        }

        wrapper.orderByDesc(Device::getCreateTime);

        Page<Device> result = deviceService.page(page, wrapper);
        return new Result(SuccessConstant.SUCCESS, "分页查询成功", result);
    }

    @ApiOperation("根据ID查询设备")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询设备：{}", id);
        Device device = deviceService.getById(id);
        return new Result(SuccessConstant.SUCCESS, "查询成功", device);
    }

    @ApiOperation("新增设备")
    @PostMapping
    public Result add(@RequestBody Device device) {
        log.info("新增设备：{}", device);
        deviceService.save(device);
        return new Result(SuccessConstant.SUCCESS, "新增成功");
    }

    @ApiOperation("更新设备")
    @PutMapping
    public Result update(@RequestBody Device device) {
        log.info("更新设备：{}", device);
        deviceService.updateById(device);
        return new Result(SuccessConstant.SUCCESS, "更新成功");
    }

    @ApiOperation("删除设备")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除设备：{}", id);
        deviceService.removeById(id);
        return new Result(SuccessConstant.SUCCESS, "删除成功");
    }








}
